/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servelt;

import DatabaseAccessing.AddUserException;
import DatabaseAccessing.Customer;
import DatabaseAccessing.CustomerTransaction;
import DatabaseAccessing.ProductModelController;
import DatabaseAccessing.TransactionModelController;
import DatabaseAccessing.UserModelController;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author MaramZahran
 */
public class CheckOut extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        UserModelController uMC= new UserModelController();
        ProductModelController pMC = new ProductModelController();
        TransactionModelController tMC = new TransactionModelController();
        HttpSession session = request.getSession(false);
        if(session!=null)
        {
            Object userObj = session.getAttribute("LOGIN_USER");
                if(userObj!=null)
                {

                    Customer user = (Customer) userObj;

                 int userID = user.getCustomerId();

            //Customer user = uMC.getUser(userID);
            //float credit = user.getCredit();

              float credit = user.getCredit();
            List userCart = tMC.getUndoneTransactions(userID);
            float check = 0;
            for(int i =0 ; i < userCart.size();i++)
            {
                CustomerTransaction transobject = (CustomerTransaction)userCart.get(i);
                check = check+ transobject.getUnitPrice()*transobject.getCount();
            }
            if(credit>=check)
            {
                credit = credit-check;
                user.setCredit(credit);
                for(int i = 0 ; i < userCart.size();i++)
                {
                    CustomerTransaction transobject = (CustomerTransaction)userCart.get(i);
                    tMC.setDone(transobject.getTransactionId());
                }
                    try {
                        uMC.editProfile(user);
                    } catch (AddUserException ex) {
                        Logger.getLogger(CheckOut.class.getName()).log(Level.SEVERE, null, ex);
                        ex.printStackTrace();
                    }
                session.removeAttribute("msg");
                response.sendRedirect("Products.jsp");
            }
            else
            {
                session.setAttribute("msg", new String("your credit is  empty please ask the admin to charge credit"));
                response.sendRedirect("CheckOut.jsp");

                //To do later
            }

        }
        else
        {
                session.setAttribute("msg", new String("Please Sign in First"));
                response.sendRedirect("CheckOut.jsp");
        }
        }
        


        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CheckOut</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CheckOut at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
